﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chbl.Models;

namespace Chbl.ViewModels
{
    public class ScheduleViewModel
    {
        private const int TimeIntervalBetweenGamesInMinutes = 70;
        private readonly IEnumerable<Game> _games;
        private Dictionary<DateTime, IEnumerable<Game>> _schedule;
        private List<DateTime> _gamesTime;
        private DateTime _gamesStartTime;

        public ScheduleViewModel(IEnumerable<Game> games, DateTime gamesStartTime)
        {
            _games = games;
            _gamesStartTime = gamesStartTime;
            InitSchedule();
        }

        public Dictionary<DateTime, IEnumerable<Game>> Schedule
        {
            get { return _schedule; }
        }

        public IEnumerable<Game> Games
        {
            get { return _games; }
        }

        public IEnumerable<DateTime> GamesTime
        {
            get { return _gamesTime; }
        }

        private void InitSchedule()
        {
            _schedule = new Dictionary<DateTime, IEnumerable<Game>>();
            IEnumerable<DateTime> tourDates = _games.Select(game => game.Date).Distinct();
            foreach (DateTime tourDate in tourDates)
            {
                DateTime date = tourDate;
                _schedule.Add(tourDate, _games.Where(g => g.Date == date).OrderBy(g => g.GameOrder));
            }
            _gamesTime = new List<DateTime>();
            IEnumerable<int> gameOrders = _games.Select(game => game.GameOrder).Distinct();
            foreach (var gameOrder in gameOrders)
            {
                _gamesTime.Add(_gamesStartTime.AddMinutes(TimeIntervalBetweenGamesInMinutes * (gameOrder - 1)));
            }
        }
    }
}